package bookez.model.service.impl;

import java.util.List;

import bookez.model.businessobject.Route;
import bookez.model.dao.RouteDao;
import bookez.model.exception.BusinessLogicLayerException;
import bookez.model.exception.DataAccessLayerException;
import bookez.model.exception.DuplicateValueException;
import bookez.model.exception.RouteExistingException;
import bookez.model.service.GenericService;
import bookez.model.service.RouteService;

public class RouteServiceImpl extends GenericService<RouteDao> implements RouteService {

	@Override
	public boolean saveRoute(Route route)
			throws BusinessLogicLayerException {
		// TODO Auto-generated method stub
		boolean result = true;
		
		try {
			/*if( route.getId() == null ) {
				Route check = getDao().findByCities( route.getFromCity(), route.getToCity() );
				if( check == null ) {
					Long id  = getDao().create( route );
					route.setId( id );
				}
				else {
					route.setId( check.getId() );
					throw new DuplicateValueException();
				}
			}
			else {
				getDao().update( route );
			}*/
			Long id = getDao().create( route );
			Long currentId = route.getId();
			if( currentId == null ) {
				route.setId( id );
			}
		}//end try
		catch( DuplicateValueException dve ) {
			result = false;
			throw new RouteExistingException( dve );
		}//end catch 
		
		catch( DataAccessLayerException dalEx ) {
			result = false;
			throw new BusinessLogicLayerException( dalEx );
		}//end catch
				
		return result;
	}//end method saveRoute

	@Override
	public List<Route> getAllRoute() throws BusinessLogicLayerException {
		// TODO Auto-generated method stub
		List<Route> result = null;
		
		try {
			result = getDao().findAll();
		}//end try
		
		catch( DataAccessLayerException dalEx ) {
			throw new BusinessLogicLayerException( dalEx );
		}//end catch
		
		return result;
	}//end getAllRoute

	@Override
	public List<Route> getRouteByFromCity(String fromCity)
			throws BusinessLogicLayerException {
		// TODO Auto-generated method stub
		List<Route> result = null;
		
		try {
			result = getDao().findByFromCity( fromCity );
		}//end try
		
		catch ( DataAccessLayerException dalEx ) {
			throw new BusinessLogicLayerException( dalEx );
		}//end catch
		
		return result;
	}//end getRouteByFromCity

	@Override
	public List<Route> getRouteByToCity(String toCity)
			throws BusinessLogicLayerException {
		// TODO Auto-generated method stub
		List<Route> result = null;
		
		try {
			result = getDao().findByToCity( toCity );
		}//end try
		
		catch ( DataAccessLayerException dalEx ) {
			throw new BusinessLogicLayerException( dalEx );
		}//end catch
		
		return result;
	}//end getRouteByToCity	
	
}//end class RouteServiceImpl
